package com.hctforgreen.greenservice;

import java.io.File;
import java.io.FileOutputStream;

import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.ThemeStoreUtil;

/**
 * 主题设置页面(软件设置页内)
 * 
 * @author Tallen Zhai
 * 
 */
public class ThemeSettingActivity extends ParentActivity {
	private static final int PHOTO_PICKED_WITH_DATA = 0x12;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_theme_setting);

		initTitleButtonBar();
		initWindow();
		initSkinLayoutSelf();
	}

	private void initSkinLayoutSelf() {
		final ImageView skin1Iv = (ImageView) findViewById(R.id.iv_skin_1);
		final ImageView skin2Iv = (ImageView) findViewById(R.id.iv_skin_2);
		final ImageView skin3Iv = (ImageView) findViewById(R.id.iv_skin_3);
		final ImageView skin4Iv = (ImageView) findViewById(R.id.iv_skin_4);
		final ImageView skin5Iv = (ImageView) findViewById(R.id.iv_skin_5);
		final ImageView skinCustomIv = (ImageView) findViewById(R.id.iv_skin_self);
		switch (ThemeStoreUtil.getSkinId(this)) {
		case R.drawable.ic_default_activity_bg:
			skin1Iv.setVisibility(View.VISIBLE);
			skin2Iv.setVisibility(View.GONE);
			skin3Iv.setVisibility(View.GONE);
			skin4Iv.setVisibility(View.GONE);
			skin5Iv.setVisibility(View.GONE);
			skinCustomIv.setVisibility(View.GONE);
			break;
		case R.drawable.ic_skin_2_bg:
			skin1Iv.setVisibility(View.GONE);
			skin2Iv.setVisibility(View.VISIBLE);
			skin3Iv.setVisibility(View.GONE);
			skin4Iv.setVisibility(View.GONE);
			skin5Iv.setVisibility(View.GONE);
			skinCustomIv.setVisibility(View.GONE);
			break;
		case R.drawable.ic_skin_3_bg:
			skin1Iv.setVisibility(View.GONE);
			skin2Iv.setVisibility(View.GONE);
			skin3Iv.setVisibility(View.VISIBLE);
			skin4Iv.setVisibility(View.GONE);
			skin5Iv.setVisibility(View.GONE);
			skinCustomIv.setVisibility(View.GONE);
			break;
		case R.drawable.ic_skin_4_bg:
			skin1Iv.setVisibility(View.GONE);
			skin2Iv.setVisibility(View.GONE);
			skin3Iv.setVisibility(View.GONE);
			skin4Iv.setVisibility(View.VISIBLE);
			skin5Iv.setVisibility(View.GONE);
			skinCustomIv.setVisibility(View.GONE);
			break;
		case R.drawable.ic_skin_5_bg:
			skin1Iv.setVisibility(View.GONE);
			skin2Iv.setVisibility(View.GONE);
			skin3Iv.setVisibility(View.GONE);
			skin4Iv.setVisibility(View.GONE);
			skin5Iv.setVisibility(View.VISIBLE);
			skinCustomIv.setVisibility(View.GONE);
			break;
		case -1:// 自定义背景
			skin1Iv.setVisibility(View.GONE);
			skin2Iv.setVisibility(View.GONE);
			skin3Iv.setVisibility(View.GONE);
			skin4Iv.setVisibility(View.GONE);
			skin5Iv.setVisibility(View.GONE);
			skinCustomIv.setVisibility(View.VISIBLE);
			break;
		}

		updateSkinLayout();
	}

	private void updateSkinLayout() {
		LinearLayout parentLyt = (LinearLayout) findViewById(R.id.lyt_parent);
		int skinId = ThemeStoreUtil.getSkinId(this);
		if (skinId != -1) {
			parentLyt.setBackgroundResource(skinId);
		} else {
			String filePath = ThemeStoreUtil
					.getSkinFilePath(ThemeSettingActivity.this);
			if (!TextUtils.isEmpty(filePath) && new File(filePath).exists()) {
				try {
					Drawable bitmapDrawable = new BitmapDrawable(
							getResources(), filePath);
					// DecodeBitmapUtil bitmapUtil = new DecodeBitmapUtil();
					parentLyt.setBackgroundDrawable(bitmapDrawable);
				} catch (Exception e) {
					e.printStackTrace();
				}
			} else {
				parentLyt
						.setBackgroundResource(R.drawable.ic_default_activity_bg);
			}
		}
	}

	private void initTitleButtonBar() {
		final TextView titleTv = (TextView) findViewById(R.id.tv_title);
		titleTv.setText(getString(R.string.theme_setting_title_hint));

		final ImageButton backBtn = (ImageButton) findViewById(R.id.btn_back);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setResult(RESULT_OK);
				finish();
			}
		});
	}

	private void initWindow() {
		final FrameLayout skin1Lyt = (FrameLayout) findViewById(R.id.lyt_skin_1);
		final FrameLayout skin2Lyt = (FrameLayout) findViewById(R.id.lyt_skin_2);
		final FrameLayout skin3Lyt = (FrameLayout) findViewById(R.id.lyt_skin_3);
		final FrameLayout skin4Lyt = (FrameLayout) findViewById(R.id.lyt_skin_4);
		final FrameLayout skin5Lyt = (FrameLayout) findViewById(R.id.lyt_skin_5);
		final FrameLayout skinCustomLyt = (FrameLayout) findViewById(R.id.lyt_skin_self);// 自定义
		skin1Lyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ThemeStoreUtil.saveSkinId(ThemeSettingActivity.this,
						"R.drawable.ic_default_activity_bg");
				initSkinLayoutSelf();
			}
		});
		skin2Lyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ThemeStoreUtil.saveSkinId(ThemeSettingActivity.this,
						"R.drawable.ic_skin_2_bg");

				initSkinLayoutSelf();
			}
		});
		skin3Lyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ThemeStoreUtil.saveSkinId(ThemeSettingActivity.this,
						"R.drawable.ic_skin_3_bg");

				initSkinLayoutSelf();
			}
		});
		skin4Lyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ThemeStoreUtil.saveSkinId(ThemeSettingActivity.this,
						"R.drawable.ic_skin_4_bg");
				initSkinLayoutSelf();
			}
		});

		skin5Lyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				ThemeStoreUtil.saveSkinId(ThemeSettingActivity.this,
						"R.drawable.ic_skin_5_bg");
				initSkinLayoutSelf();
			}
		});

		skinCustomLyt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {// 选择本体图片或者拍照
				startActivityForResult(getPhotoPickIntent(),
						PHOTO_PICKED_WITH_DATA);
			}
		});
	}

	private Intent getPhotoPickIntent() {

		Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
		intent.setType("image/*");
		return intent;
	}

	@Override
	public void onBackPressed() {
		setResult(RESULT_OK);
		finish();
		super.onBackPressed();
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

	/**
	 * 压缩图片
	 */
	private String zipPhoto(final String photoFile) {
		if (photoFile == null || photoFile.trim().length() == 0) {
			return "";
		}
		File file = new File(photoFile);
		if (!file.exists()) {
			return "";
		}
		File resFile = new File(HctConstants.DEFAULT_ROOT_PATH
				+ HctConstants.DEFAULT_IMG_STORE_PATH + file.getName());
		try {
			if (resFile.getParentFile() == null
					|| !resFile.getParentFile().exists()) {
				resFile.getParentFile().mkdirs();
			}
			if (!resFile.exists()) {
				resFile.createNewFile();
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}

		BitmapFactory.Options options = new BitmapFactory.Options();
		if (file.length() > 10 * 1024 * 1024) {
			options.inSampleSize = 30;
		} else if (file.length() > 6 * 1024 * 1024) {
			options.inSampleSize = 20;
		} else if (file.length() > 2 * 1024 * 1024) {
			options.inSampleSize = 10;
		} else if (file.length() > 1024 * 1024) {
			options.inSampleSize = 5;
		} else if (file.length() > 1024 * 512) {
			options.inSampleSize = 2;
		} else {
			options.inSampleSize = 1;
		}
		Bitmap bmp = BitmapFactory.decodeFile(photoFile, options);
		try {
			bmp.compress(CompressFormat.JPEG, 75, new FileOutputStream(resFile));
			// FileOutputStream fos = new FileOutputStream(resFile);
			// fos.write(baos.toByteArray());
			// fos.flush();
			// fos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

		return resFile.getAbsolutePath();

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		switch (requestCode) {
		case PHOTO_PICKED_WITH_DATA: // 相册 图片压缩
			if (resultCode == RESULT_OK) {
				Uri uri = data.getData();
				String selectedImagePath = getPath(uri);

				if (selectedImagePath == null
						|| "".equals(selectedImagePath.trim())) {
					Toast.makeText(this,
							getString(R.string.feed_back_get_image_fail),
							Toast.LENGTH_LONG).show();
					return;
				}
				String filePath = zipPhoto(selectedImagePath);
				if (!TextUtils.isEmpty(filePath)) {
					ThemeStoreUtil.saveSkinId(ThemeSettingActivity.this,
							"native_file");
					ThemeStoreUtil.saveSkinFilePath(ThemeSettingActivity.this,
							filePath);
					initSkinLayoutSelf();
				}

			}
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
}
